import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;

import java.io.IOException;

import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Base64;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.DateTimeZone;
import org.elasticsearch.common.xcontent.XContentFactory;


public class Maping {

	
	public static void main(String[] args) throws IOException {
		// createIndex("test");
		Client client =	ESUtils.getClient();
		 GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings("lisheng").execute().actionGet();
		 System.out.println(getMappingsResponse.mappings().get("lisheng").get("test").source());
		 boolean stop = true;
		 if(stop){
			 return;
		 }
		 client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();

	        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
	                .startObject("_source").field("enabled", false).endObject()
	                .startObject("byte_field").field("type", "byte").field("store", "yes").endObject()
	                .startObject("short_field").field("type", "short").field("store", "yes").endObject()
	                .startObject("integer_field").field("type", "integer").field("store", "yes").endObject()
	                .startObject("long_field").field("type", "long").field("store", "yes").endObject()
	                .startObject("float_field").field("type", "float").field("store", "yes").endObject()
	                .startObject("double_field").field("type", "double").field("store", "yes").endObject()
	                .startObject("date_field").field("type", "date").field("store", "yes").endObject()
	                .startObject("boolean_field").field("type", "boolean").field("store", "yes").endObject()
	                .startObject("binary_field").field("type", "binary").field("store", "yes").endObject()
	                .endObject().endObject().endObject().string();

	        ESUtils.getClient().admin().indices().preparePutMapping().setType("type1").setSource(mapping).execute().actionGet();
	        client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject()
	                .field("byte_field", (byte) 1)
	                .field("short_field", (short) 2)
	                .field("integer_field", 3)
	                .field("long_field", 4l)
	                .field("float_field", 5.0f)
	                .field("double_field", 6.0d)
	                .field("date_field", Joda.forPattern("dateOptionalTime").printer().print(new DateTime(2012, 3, 22, 0, 0, DateTimeZone.UTC)))
	                .field("boolean_field", true).field("analyzer", "simple")
	                .field("binary_field", Base64.encodeBytes("testing text".getBytes("UTF8")))
	                .endObject()).execute().actionGet();

	        client.admin().indices().prepareRefresh().execute().actionGet();

	        SearchResponse searchResponse = client.prepareSearch().setQuery(matchAllQuery())
	                .addField("byte_field")
	                .addField("short_field")
	                .addField("integer_field")
	                .addField("long_field")
	                .addField("float_field")
	                .addField("double_field")
	                .addField("date_field")
	                .addField("boolean_field")
	                .addField("binary_field")
	                .execute().actionGet();
	        System.out.println(searchResponse);
	}
}
